﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using Wowhead.Common;

namespace Wowhead.WebControls
{
    public class ItemLink : Control, INamingContainer
    {
        protected override void OnInit(EventArgs e)
        {
            this.EnableViewState = false;
            if (!Page.ClientScript.IsClientScriptIncludeRegistered("wowhead"))
                Page.ClientScript.RegisterClientScriptInclude("wowhead", "http://www.wowhead.com/widgets/power.js");
            Page.RegisterRequiresControlState(this);
            base.OnInit(e);
        }

        private bool[] IsCurrent = new bool[2] { true, true };
        public int ItemID { get; set; }

        public string Text { get; set; }

        private HyperLink InnerLink;

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            InnerLink = new HyperLink();
            
            if (!string.IsNullOrEmpty(this.ClientID))
                InnerLink.ID = this.ClientID;

            InnerLink.NavigateUrl = string.Format("http://www.wowhead.com/?item={0}", ItemID);

            if (string.IsNullOrEmpty(this.Text))
                InnerLink.Text = this.ItemName;
            else
                InnerLink.Text = this.Text;

            InnerLink.CssClass = string.Format("q{0}", this.Quality);
            
        }

        public override void RenderControl(HtmlTextWriter writer)
        {
            InnerLink.RenderControl(writer);
        }

        public void LoadItem(int itemID)
        {
            IsCurrent[0] = false;
            IsCurrent[1] = false;
            this.ItemID = itemID;
        }

        private XElement _WowheadXml;
        private XElement WowheadXml
        {
            get
            {
                if (_WowheadXml == null)// || !IsCurrent)
                {
                    _WowheadXml = Utils.GetWowheadXmlObject(WowheadObjectType.Item, this.ItemID);
                }
                return _WowheadXml;
            }
        }

        private int? _Quality;
        public int Quality
        {
            get
            {
                if (!_Quality.HasValue || !IsCurrent[0])
                {
                    _Quality = int.Parse(WowheadXml.Element("item").Element("quality").Attribute("id").Value);
                    IsCurrent[0] = true;
                }
                return _Quality.Value;
            }
            private set { _Quality = value; }
        }

        private string _ItemName;
        public string ItemName
        {
            get
            {
                if (string.IsNullOrEmpty(_ItemName) || !IsCurrent[1])
                {
                    _ItemName = WowheadXml.Element("item").Element("name").Value;
                    IsCurrent[1] = true;
                }
                return _ItemName;
            }
            private set
            {
                _ItemName = value;
            }
        }

        #region ControlState
        protected override void LoadControlState(object savedState)
        {
            object[] rawState = (object[])savedState;
            base.LoadControlState(rawState[0]);
            this.Quality = (int)rawState[1];
            this.ItemName = (string)rawState[2];
            //this._ItemID = (int)rawState[3];
        }

        protected override object SaveControlState()
        {
            object[] rawState = new object[4];
            rawState[0] = base.SaveControlState();
            rawState[1] = this.Quality;
            rawState[2] = this.ItemName;
            //rawState[3] = this._ItemID;
            return rawState;
        }
        #endregion
    }
}
